<template>
  <div>
    <el-dialog title="添加秒杀|修改秒杀" :visible.sync="flag.isShow" @opened="opened" @closed="closed">
      {{form}}
      <el-form :model="form">
        <el-form-item label="限时秒杀名称" label-width="120px">
          <el-input v-model="form.title" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="开始结束时间" label-width="120px">
          <el-date-picker
            v-model="time"
            type="datetimerange"
            value-format="timestamp"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="一级分类" label-width="120px">
          <el-select
            v-model="form.first_cateid"
            placeholder="请选择活动区域"
            @change="reqcatelist2(form.first_cateid)"
          >
            <el-option
              :label="item.catename"
              v-for="item in cateList1"
              :key="item.id"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="二级分类" label-width="120px">
          <el-select
            v-model="form.second_cateid"
            placeholder="请选择活动区域"
            @change="reqTwoGoods(form.second_cateid)"
          >
            <el-option
              :label="item.catename"
              v-for="item in cateList2"
              :key="item.id"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="活动商品" label-width="120px">
          <el-select v-model="form.giidsid" placeholder="请选择活动区域">
            <el-option
              :label="item.goodsname"
              v-for="item in twoGoods"
              :key="item.id"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态" label-width="120px">
          <el-switch
            v-model="form.status"
            active-color="#13ce66"
            :active-value="1"
            :inactive-value="2"
          ></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取消</el-button>
        <el-button type="primary" @click="change">修改</el-button>
        <el-button type="primary" @click="add">添加</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { mapGetters, mapActions } from "vuex";
export default {
  data() {
    return {
      form: {
        title: "",
        begintime: "",
        endtime: "",
        first_cateid: "",
        second_cateid: "",
        giidsid: "",
        status: 1
      },
      time: ""
    };
  },
  props: ["flag"],
  computed: {
    ...mapGetters({
      cateList1: "seckill/cateList1",
      cateList2: "seckill/cateList2",
      twoGoods: "seckill/twoGoods"
    })
  },
  components: {},
  methods: {
    ...mapActions({
      reqcatelist1: "seckill/reqcatelist1",
      reqcatelist2: "seckill/reqcatelist2",
      reqTwoGoods: "seckill/reqTwoGoods",
      seckAdd: "seckill/seckAdd"
    }),
    // 取消
    cancel() {
      this.flag.isShow = false;
    },
    // 修改
    change() {
      this.flag.isShow = false;
    },
    // 添加
    add() {
      this.flag.isShow = false;
      this.form.begintime = this.time[0];
      this.form.endtime = this.time[1];
      this.seckAdd(this.form);
    },
    // 打开时
    opened() {
      this.reqcatelist1();
    },
    // 关闭时
    closed() {
      if (!this.flag.add) {
        this.clear();
      }
    },
    // 清空
    clear() {
      this.form = {
        title: "",
        begintime: "",
        endtime: "",
        first_cateid: "",
        second_cateid: "",
        giidsid: "",
        status: 1
      };
      this.time = "";
    }
  },
  mounted() {}
};
</script>
<style lang='less' scoped>
.el-date-editor--datetimerange.el-input,
.el-date-editor--datetimerange.el-input__inner {
  width: auto;
}
</style>